"""
JAR包对比结果Excel生成服务
"""
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
from openpyxl.utils import get_column_letter
from typing import Dict, List


class CompareExcelService:
    """JAR包对比结果Excel生成服务类"""
    
    @staticmethod
    def generate_excel(compare_result: Dict, output_path: str) -> None:
        """
        生成JAR包对比结果Excel文件
        
        Args:
            compare_result: 对比结果字典，包含：
                - only_in_jar1: 只在JAR1中的文件
                - only_in_jar2: 只在JAR2中的文件
                - different_content: 内容不同的文件
                - same_content: 内容相同的文件
                - summary: 统计信息
                - jar1_name: JAR1名称
                - jar2_name: JAR2名称
            output_path: 输出文件路径
        """
        wb = Workbook()
        
        # 删除默认工作表
        wb.remove(wb.active)
        
        # 创建各个工作表
        CompareExcelService._create_summary_sheet(wb, compare_result)
        CompareExcelService._create_only_in_jar1_sheet(wb, compare_result)
        CompareExcelService._create_only_in_jar2_sheet(wb, compare_result)
        CompareExcelService._create_different_content_sheet(wb, compare_result)
        CompareExcelService._create_same_content_sheet(wb, compare_result)
        
        # 保存Excel文件
        wb.save(output_path)
    
    @staticmethod
    def _create_summary_sheet(wb: Workbook, compare_result: Dict) -> None:
        """创建统计信息工作表"""
        ws = wb.create_sheet("对比统计", 0)
        
        summary = compare_result['summary']
        jar1_name = compare_result['jar1_name']
        jar2_name = compare_result['jar2_name']
        
        # 标题
        ws.merge_cells('A1:B1')
        ws['A1'] = 'JAR包对比统计报告'
        ws['A1'].font = Font(bold=True, size=16)
        ws['A1'].alignment = Alignment(horizontal='center', vertical='center')
        
        # 基本信息
        row = 3
        ws[f'A{row}'] = '对比信息'
        ws[f'A{row}'].font = Font(bold=True, size=12)
        row += 1
        
        ws[f'A{row}'] = f'{jar1_name} 总文件数:'
        ws[f'B{row}'] = summary['total_files_jar1']
        row += 1
        
        ws[f'A{row}'] = f'{jar2_name} 总文件数:'
        ws[f'B{row}'] = summary['total_files_jar2']
        row += 1
        
        ws[f'A{row}'] = '对比文件总数:'
        ws[f'B{row}'] = summary['total_compared']
        row += 2
        
        # 对比结果统计
        ws[f'A{row}'] = '对比结果'
        ws[f'A{row}'].font = Font(bold=True, size=12)
        row += 1
        
        # 内容相同的文件
        ws[f'A{row}'] = '内容相同的文件:'
        ws[f'B{row}'] = summary['same_content_count']
        ws[f'B{row}'].fill = PatternFill(start_color="C6EFCE", end_color="C6EFCE", fill_type="solid")
        row += 1
        
        # 内容不同的文件
        ws[f'A{row}'] = '内容不同的文件:'
        ws[f'B{row}'] = summary['different_content_count']
        ws[f'B{row}'].fill = PatternFill(start_color="FFC7CE", end_color="FFC7CE", fill_type="solid")
        row += 1
        
        # 只在JAR1中的文件
        ws[f'A{row}'] = f'只在 {jar1_name} 中的文件:'
        ws[f'B{row}'] = summary['only_in_jar1_count']
        ws[f'B{row}'].fill = PatternFill(start_color="FFEB9C", end_color="FFEB9C", fill_type="solid")
        row += 1
        
        # 只在JAR2中的文件
        ws[f'A{row}'] = f'只在 {jar2_name} 中的文件:'
        ws[f'B{row}'] = summary['only_in_jar2_count']
        ws[f'B{row}'].fill = PatternFill(start_color="FFEB9C", end_color="FFEB9C", fill_type="solid")
        
        # 调整列宽
        ws.column_dimensions['A'].width = 30
        ws.column_dimensions['B'].width = 20
    
    @staticmethod
    def _create_only_in_jar1_sheet(wb: Workbook, compare_result: Dict) -> None:
        """创建只在JAR1中的文件工作表"""
        ws = wb.create_sheet("只在JAR1中")
        
        jar1_name = compare_result['jar1_name']
        files = compare_result['only_in_jar1']
        
        # 表头
        headers = ['文件路径', 'JAR包位置', '文件大小(字节)']
        ws.append(headers)
        
        # 设置表头样式
        header_fill = PatternFill(start_color="FFEB9C", end_color="FFEB9C", fill_type="solid")
        for cell in ws[1]:
            cell.font = Font(bold=True)
            cell.alignment = Alignment(horizontal='center')
            cell.fill = header_fill
        
        # 添加数据
        for file_info in files:
            ws.append([
                file_info['path'],
                file_info['jar_name'],
                file_info['size']
            ])
        
        # 调整列宽
        ws.column_dimensions['A'].width = 60
        ws.column_dimensions['B'].width = 40
        ws.column_dimensions['C'].width = 15
    
    @staticmethod
    def _create_only_in_jar2_sheet(wb: Workbook, compare_result: Dict) -> None:
        """创建只在JAR2中的文件工作表"""
        ws = wb.create_sheet("只在JAR2中")
        
        jar2_name = compare_result['jar2_name']
        files = compare_result['only_in_jar2']
        
        # 表头
        headers = ['文件路径', 'JAR包位置', '文件大小(字节)']
        ws.append(headers)
        
        # 设置表头样式
        header_fill = PatternFill(start_color="FFEB9C", end_color="FFEB9C", fill_type="solid")
        for cell in ws[1]:
            cell.font = Font(bold=True)
            cell.alignment = Alignment(horizontal='center')
            cell.fill = header_fill
        
        # 添加数据
        for file_info in files:
            ws.append([
                file_info['path'],
                file_info['jar_name'],
                file_info['size']
            ])
        
        # 调整列宽
        ws.column_dimensions['A'].width = 60
        ws.column_dimensions['B'].width = 40
        ws.column_dimensions['C'].width = 15
    
    @staticmethod
    def _create_different_content_sheet(wb: Workbook, compare_result: Dict) -> None:
        """创建内容不同的文件工作表"""
        ws = wb.create_sheet("内容不同")
        
        jar1_name = compare_result['jar1_name']
        jar2_name = compare_result['jar2_name']
        files = compare_result['different_content']
        
        # 表头
        headers = ['文件路径', f'{jar1_name}位置', f'{jar1_name}大小', 
                  f'{jar1_name}哈希值', f'{jar2_name}位置', 
                  f'{jar2_name}大小', f'{jar2_name}哈希值']
        ws.append(headers)
        
        # 设置表头样式
        header_fill = PatternFill(start_color="FFC7CE", end_color="FFC7CE", fill_type="solid")
        for cell in ws[1]:
            cell.font = Font(bold=True)
            cell.alignment = Alignment(horizontal='center')
            cell.fill = header_fill
        
        # 添加数据
        for file_info in files:
            ws.append([
                file_info['path'],
                file_info['jar1_name'],
                file_info['jar1_size'],
                file_info['jar1_hash'],
                file_info['jar2_name'],
                file_info['jar2_size'],
                file_info['jar2_hash']
            ])
        
        # 调整列宽
        ws.column_dimensions['A'].width = 50
        ws.column_dimensions['B'].width = 30
        ws.column_dimensions['C'].width = 15
        ws.column_dimensions['D'].width = 35
        ws.column_dimensions['E'].width = 30
        ws.column_dimensions['F'].width = 15
        ws.column_dimensions['G'].width = 35
    
    @staticmethod
    def _create_same_content_sheet(wb: Workbook, compare_result: Dict) -> None:
        """创建内容相同的文件工作表"""
        ws = wb.create_sheet("内容相同")
        
        jar1_name = compare_result['jar1_name']
        jar2_name = compare_result['jar2_name']
        files = compare_result['same_content']
        
        # 表头
        headers = ['文件路径', f'{jar1_name}位置', f'{jar2_name}位置', 
                  '文件大小(字节)', '文件哈希值']
        ws.append(headers)
        
        # 设置表头样式
        header_fill = PatternFill(start_color="C6EFCE", end_color="C6EFCE", fill_type="solid")
        for cell in ws[1]:
            cell.font = Font(bold=True)
            cell.alignment = Alignment(horizontal='center')
            cell.fill = header_fill
        
        # 添加数据
        for file_info in files:
            ws.append([
                file_info['path'],
                file_info['jar1_name'],
                file_info['jar2_name'],
                file_info['size'],
                file_info['hash']
            ])
        
        # 调整列宽
        ws.column_dimensions['A'].width = 50
        ws.column_dimensions['B'].width = 30
        ws.column_dimensions['C'].width = 30
        ws.column_dimensions['D'].width = 15
        ws.column_dimensions['E'].width = 35

